package com.htt.bms.service.impl.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htt.bms.common.exception.BusinessException;
import com.htt.bms.dao.system.SysUserDao;
import com.htt.bms.domain.sys.SysOrg;
import com.htt.bms.domain.sys.SysUser;
import com.htt.bms.domain.sys.SysUserOrg;
import com.htt.bms.domain.sys.SysUserRole;
import com.htt.bms.domain.sys.dto.SysUserDto;
import com.htt.bms.service.system.SysOrgService;
import com.htt.bms.service.system.SysUserOrgService;
import com.htt.bms.service.system.SysUserRoleService;
import com.htt.bms.service.system.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 侯婷婷
 * @version 1.0
 * @description
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {


    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysUserOrgService sysUserOrgService;

    @Resource
    private SysOrgService sysOrgService;

    /**
     * 用户列表
     * @param dto
     * @return
     */
    @Override
    public List<SysUser> list(SysUserDto dto) {
        //通过LambadaQueryWraooer查询数据库
        //TODO 弄懂,自己写...这里需要完成token的配置
        /*LambdaQueryWrapper queryWrapper = null;
        if (Objects.equals(0, SecurityUserUtils.getUser().getType())) {
            queryWrapper = Wrappers.<SysUser>lambdaQuery()
                    .eq(SysUser::getDelFlag, 0)
                    .like(Objects.nonNull(dto.getNickname()), SysUser::getNickname, dto.getNickname())
                    .orderByDesc(SysUser::getCreateTime, SysUser::getUpdateTime);
        } else {
            queryWrapper = Wrappers.<SysUser>lambdaQuery()
                    .eq(SysUser::getDelFlag, 0)
                    .in(SysUser::getOrgId, SecurityUserUtils.getUser().getOrgIds())
                    .like(Objects.nonNull(dto.getNickname()), SysUser::getNickname, dto.getNickname())
                    .orderByDesc(SysUser::getCreateTime, SysUser::getUpdateTime);
        }
        return this.baseMapper.selectList(queryWrapper);*/
        return null;

    }

    /**
     * 登录-根据用户名获取用户信息
     * @param username
     * @return
     */
    @Override
    public SysUser login(String username) {
        //根据username查询sysUser
        SysUser user = this.baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        if (Objects.nonNull(user)){
            //获取角色
            SysUserRole role = sysUserRoleService.getOne(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId,user.getId()));
            if (Objects.isNull(role) || Objects.isNull(role.getRoleId())){
                log.error("====={}================无权限====================", username);
                throw new RuntimeException("暂无登录权限，请联系管理员！");
            }
            user.setRole(String.valueOf(role.getRoleId()));

            //获取组织
            SysUserOrg org = sysUserOrgService.getOne(Wrappers.<SysUserOrg>lambdaQuery().eq(SysUserOrg::getUserId, user.getId()));
            if (Objects.isNull(org) || Objects.isNull(org.getOrgId())){
                log.error("====={}================无组织====================", username);
                throw new BusinessException("暂无登录权限，请联系管理员！");
            }
            user.setOrgId(org.getOrgId());

            //获取组织权限
            List<SysOrg> orgs = sysOrgService.getOrgChildren(org.getOrgId());
            if(Objects.nonNull(orgs) && orgs.size() > 0){
                List<Integer> orgIds = orgs.stream().map(SysOrg::getId).collect(Collectors.toList());
                orgIds.add(org.getOrgId());
                user.setOrgIds(orgIds);
            } else {
                user.setOrgIds(Arrays.asList(org.getOrgId()));
            }
        }
        return user;
    }
}
